<?php
/**
 * Print recordset
 *
 * @author Arkadiusz Bisaga <abisaga@telaxus.com>
 * @copyright Copyright &copy; 2006, Telaxus LLC
 * @version 1.0
 * @license MIT
 * @package epesi-utils
 * @subpackage RecordBrowser
 */
if(!isset($_REQUEST['cid']) || !isset($_REQUEST['key'])) die('Invalid usage - missing param');
$cid = $_REQUEST['cid'];
$key = $_REQUEST['key'];
$employee = $_REQUEST['employee']; 
$date = date('d-m-Y');

define('CID', $cid);
define('READ_ONLY_SESSION',true);
require_once('../../../include.php');

ModuleManager::load_modules();
set_time_limit(0);

$tcpdf = Libs_TCPDFCommon::new_pdf('L');

$employee = Utils_RecordBrowserCommon::get_record('contact', $employee);

$datetime = new DateTime($date);
Libs_TCPDFCommon::prepare_header($tcpdf, 'Bổ sung hồ sơ/thông tin', 'Ngày: ' . $date . '        Nhân viên: ' . $employee['last_name'] . ' ' . $employee['first_name'] , false);

Libs_TCPDFCommon::SetFont($tcpdf, Libs_TCPDFCommon::$default_font, '', 13);

$recs = DB::Execute("
				select  sctv_maintenanceinformationrequest_data_1.id,
						sctv_maintenance_data_1.f_maintenance_code maintenance_code,
						sctv_maintenance_data_1.f_customer_name customer_name,
						sctv_maintenance_data_1.f_address address,
						sctv_street_data_1.f_name as street,
						sctv_maintenance_data_1.f_phone phone,
						deployment_employee.id deployment_employee_id,
						deployment_employee.f_last_name || ' ' || deployment_employee.f_first_name as deployment_employee_name,
						sctv_information_data_1.f_name information,
						sctv_informationgroup_data_1.f_name information_group,
						sctv_maintenanceinformationrequest_data_1.f_note note,
						sctv_maintenanceinformationrequest_data_1.f_status status
				from sctv_maintenanceinformationrequest_data_1
				join sctv_information_data_1 on sctv_information_data_1.id = sctv_maintenanceinformationrequest_data_1.f_information
				join sctv_informationgroup_data_1 on sctv_informationgroup_data_1.id = sctv_information_data_1.f_information_group  
				join sctv_maintenance_data_1 on sctv_maintenance_data_1.id = sctv_maintenanceinformationrequest_data_1.f_maintenance and sctv_maintenance_data_1.f_deployment_employee = %d
				join contact_data_1 deployment_employee on deployment_employee.id = sctv_maintenance_data_1.f_deployment_employee
				join sctv_street_data_1 on sctv_street_data_1.id = sctv_maintenance_data_1.f_street
				join sctv_area_data_1 on sctv_area_data_1.id = sctv_street_data_1.f_area
				where sctv_maintenanceinformationrequest_data_1.active = 1 and sctv_maintenanceinformationrequest_data_1.f_status = '0'
			", array($employee['id'])
			);

$count = 0;
			
foreach($recs as $key=>$value)
{
	if($count%20 == 0)
	{
		Libs_TCPDFCommon::add_page($tcpdf);
		$html = '<ol>';
	}
	$html .= '<li>';
			 
	$html .='#' . $value['maintenance_code'] . ($value['customer_name']==null?'': '; Tên: ' . $value['customer_name']). '; địa chỉ: ' . $value['address'] . ' ' . $value['street'] . '; điện thoại: ' . $value['phone'] . '; Yêu cầu: ' . $value['information'] . ($value['note']==null?'':'; ghi chú: ' . $value['note']);
				
	$html .= '</li>';
	
	$count = $count + 1;
	if($count%20 == 0)
	{
		$html .= '</ol>';		
		$html = Libs_TCPDFCommon::stripHTML(str_replace(array('<br>','&nbsp;'),array('<br/>',' '),$html));
		Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);		
	}
}
if($count%20 > 0)
{
	$html .= '</ol>';

	$html = Libs_TCPDFCommon::stripHTML(str_replace(array('<br>','&nbsp;'),array('<br/>',' '),$html));
	Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);	
}

$buffer = Libs_TCPDFCommon::output($tcpdf);

header('Content-Type: application/pdf');
header('Content-Length: '.strlen($buffer));
header('Content-disposition: inline; filename="recordset_'.$tab.'.pdf"');

print($buffer);

?>
